package segger.vasttrafik;

import java.util.Calendar;
import java.util.HashMap;

import org.apache.log4j.Logger;

public class Tram {
    private static final Logger logger = Logger.getLogger(Tram.class);
    
    private static final String NAME = "name";
    private static final String TRACK = "track";
    private static final String DATE = "date";
    private static final String TIME = "time";
    private static final String RT_DATE = "rtDate";
    private static final String RT_TIME = "rtTime";
    private static final String DIRECTION = "direction";

    private String name, track, date, time, rtDate, rtTime, direction = null;
    private Calendar clockTime, realTime = null;

    public Tram(HashMap<String, String> attr) {
        this.name = attr.get(NAME);
        this.track = attr.get(TRACK);
        this.date = attr.get(DATE);
        this.time = attr.get(TIME);
        this.rtDate = attr.get(RT_DATE);
        this.rtTime = attr.get(RT_TIME);
        this.direction = attr.get(DIRECTION);

        calcTimes();
    }

    /* testing purpose */
    int[] transform(String[] input) {
        int[] tmp = new int[input.length];
        for (int i = 0; i < input.length; i++) {
            tmp[i] = Integer.valueOf(input[i]);
        }
        return tmp;
    }

    private void calcTimes() {
        if(rtDate != null && rtTime != null) {
            logger.debug("real time: " + rtDate + " " + rtTime);
            realTime = calcTime(rtDate, rtTime);
        }
        
        if(date != null && time != null) {
            logger.debug("clock time: " + date + " " + time);
            clockTime = calcTime(date, time);
        }
    }
    
    /* testing purpose */
    Calendar calcTime(String date, String time) {
        String[] dTmp = date.split("-");
        String[] tTmp = time.split(":");
        
        int[] dateTmp = transform(dTmp);
        int[] timeTmp = transform(tTmp);

        Calendar tmp = Calendar.getInstance();
        int year = dateTmp[0];
        int month = dateTmp[1] - 1; // January = 0
        int day = dateTmp[2];
        int hour = timeTmp[0];
        int minute = timeTmp[1];
        tmp.set(year, month, day, hour, minute);
        return tmp;
    }
    
    public String getName() {
        return name;
    }

    public String getTrack() {
        return track;
    }

    public String getDirection() {
        return direction;
    }

    public Calendar getRealTime() {
        return realTime;
    }

    public Calendar getClockTime() {
        return clockTime;
    }
    
    private long getMinutes(long then, long now) {
        long diffMillis = then - now;
        long diffMinutes = diffMillis / (60*1000);
        return diffMinutes;
    }
    
    private long getMinutesLeaveTime() {
        Calendar now = Calendar.getInstance();
        long nowMillis = now.getTimeInMillis();
        long tramMillis = realTime.getTimeInMillis();
        return getMinutes(tramMillis, nowMillis);
    }
    
    private long getMinutesDelayedTime() {
        if(realTime != null && clockTime != null) {
            long rtMillis = realTime.getTimeInMillis();
            long ctMillis = clockTime.getTimeInMillis();
            return getMinutes(rtMillis, ctMillis);
        }
        return 0;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Tram) {
            Tram other = (Tram) obj;
            if (name.equalsIgnoreCase(other.name)
                    && direction.equalsIgnoreCase(other.direction)
                    && date.equals(other.date) && time.equals(other.time)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int hashCode() {
        return name.hashCode() + direction.hashCode() + date.hashCode()
                + time.hashCode();
    }

    public String getDefaultOutput() {
        StringBuilder builder = new StringBuilder();
        long leaveTime = getMinutesLeaveTime();
        String minutes;
        //TODO if leaveTime <= 0 : label?
        if(leaveTime == 1) {
            minutes = Utils.get("label.minute");
        } else {
            minutes = Utils.get("label.minutes");
        }
        builder.append(getName());
        builder.append(" ");
        builder.append(Utils.get("label.toDestination"));
        builder.append(" ");
        builder.append(getDirection());
        builder.append(" ");
        builder.append(Utils.get("label.leavesIn"));
        builder.append(" ");
        builder.append(leaveTime + " " + minutes);
        return builder.toString();
    }
    
    public String getDefaultLongOutput() {
        StringBuilder builder = new StringBuilder();
        long delayedTime = getMinutesDelayedTime();
        String minutes;
        if(delayedTime == 1) {
            minutes = Utils.get("label.minute");
        } else {
            minutes = Utils.get("label.minutes");
        }
        builder.append(getDefaultOutput());
        builder.append(" ");
        builder.append(Utils.get("label.fromTrack"));
        builder.append(" ");
        builder.append(getTrack());
        builder.append(", ");
        builder.append(delayedTime + " " + minutes);
        builder.append(" ");
        builder.append(Utils.get("label.delayed"));
        return builder.toString();
    }
}
